﻿package  
{
	import Box2D.Collision.Shapes.*;
	import Box2D.Dynamics.*;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author ...
	 */
	public class ball
	{
		public var m_pos:cartesian;
		public var m_radius:Number;
		public var m_angle:Number = 0;
		public var m_ballBody:b2Body;
		
		public function ball(x:Number, y:Number, radius:Number, world:b2World) 
		{
		
			m_pos = new cartesian(x, y);
			m_radius = radius;
			
			var ballShapeDef:b2CircleDef = new b2CircleDef();
			ballShapeDef.radius = radius / main.m_physScale;
			ballShapeDef.density = 2;
			//ballShapeDef.isSensor = true;
			
			var ballBodyDef:b2BodyDef = new b2BodyDef();
			ballBodyDef.position.Set(x / main.m_physScale, y / main.m_physScale);
			ballBodyDef.userData = this;
			
			m_ballBody = world.CreateBody(ballBodyDef);
			m_ballBody.CreateShape(ballShapeDef);
			m_ballBody.SetMassFromShapes();

			
		}
		
		public function update():void
		{
			// Convert Physics Coord System to "Real" Coord System
			m_pos.x = m_ballBody.GetPosition().x * main.m_physScale;
			m_pos.y = m_ballBody.GetPosition().y * main.m_physScale;
			m_angle = m_ballBody.GetAngle() * Math.PI / 180;
			
		}
		
		public function draw(vSprite:Sprite, cam:camera):void
		{
			var r:Number = m_radius * cam.zoom;
			var col:Number =  16711680;
		
			var kx:Array = new Array(1.0, .924, .707, .383, 0, -.383, -.707, -.924, -1.0, -.924, -.707, -.383, 0, .383, .707, .924, 1.0); 
			var ky:Array = new Array(0, .383, .707, .924, 1.0, .924, .707, .383, 0, -.383, -.707, -.924, -1.0, -.924, -.707, -.383, 0);
			
			
			vSprite.graphics.lineStyle(1, col);
			vSprite.graphics.moveTo(m_pos.toScreen(cam).x + r, m_pos.toScreen(cam).y);
			for (var i:int = 0; i < 17; i++)
			{
				
				i / 2 == Math.floor(i / 2) ? (vSprite.graphics.lineTo(m_pos.toScreen(cam).x + r * kx[i], m_pos.toScreen(cam).y + r * ky[i])) : (vSprite.graphics.moveTo(m_pos.toScreen(cam).x + r * kx[i], m_pos.toScreen(cam).y + r * ky[i]));
			} 
			
			
		}
		
	}
	
}